import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:freemusic/components/Common.dart';
import 'package:freemusic/components/InkWellContainer.dart';
import 'package:freemusic/model/AbstractSong.dart';
import 'package:freemusic/page/songsheet/SongSheetList.dart';
import 'package:freemusic/services/GedanServices.dart';
import 'package:freemusic/services/SongServices.dart';
import 'package:freemusic/utils/Util.dart';

enum SongItemType {
  Recent,
  Local,
  Gedan,
  Diss,
}

class SongItem extends StatelessWidget {
  final AbstractSong song;
  final int index;
  final int gedanId;
  final double itemHeight = 60.0;
  final String playingHash;
  final SongItemType type;

  const SongItem({
    Key key,
    this.song,
    this.index,
    this.gedanId,
    this.playingHash,
    @required this.type,
  }) : super(key: key);

  void _handleMoreOnTap(BuildContext context) {
    showModalBottomSheet(
        context: context,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(12),
            topRight: Radius.circular(12),
          ),
        ),
        builder: (context) {
          return _SongItemBottomSheet(
            song: song,
            gedanId: gedanId,
            type: type,
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    Color wordColor = Util.wordColor;

    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onLongPress: (){
        this._handleMoreOnTap(context);
      },
      onTap: () {
        SongServices.getInstace().playSong(song);
      },
      child: Container(
        height: itemHeight,
        child: Row(
          children: <Widget>[
            Expanded(
              child: Row(
                children: <Widget>[
                  Container(
                    height: 40,
                    width: 60,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        playingHash == song.hash
                            ? Image.asset(
                                "images/laba.png",
                                width: 24,
                              )
                            : Text(
                                "$index",
                                style:
                                    TextStyle(fontSize: 18, color: wordColor),
                              ),
                      ],
                    ),
                  ),
                  Expanded(
                    child: SongDetail(
                      songName: song.songName,
                      singerName: song.singerName,
                    ),
                  ),
                ],
              ),
            ),
            GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () {
                this._handleMoreOnTap(context);
              },
              child: Container(
                padding: EdgeInsets.all(12),
                child: Icon(
                  Icons.more_vert,
                  color: wordColor,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class _SongItemBottomSheet extends StatelessWidget {
  final AbstractSong song;
  final int gedanId;
  final double _iconSize = 24.0;
  final SongItemType type;

  const _SongItemBottomSheet({Key key, this.song, this.gedanId, this.type})
      : super(key: key);

  List<Widget> _renderClick(BuildContext context) {
    Color themeColor = Util.themeColor;
    List<Widget> widgets = List();
    widgets
      ..add(InkWellContainer(
        image: Image.asset(
          "images/add-to-playlist.png",
          width: _iconSize,
        ),
        text: "下一首播放",
        onTap: () {
          SongServices.getInstace().addNextPlay(song);
          Fluttertoast.showToast(
            msg: "已添加到播放列表",
          );
        },
      ))
      ..add(InkWellContainer(
        image: Icon(
          Icons.add_box,
          color: themeColor,
        ),
        text: "加到歌单",
        onTap: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => SongSheetListPage(
                waitAddSong: song,
              ),
            ),
          );
        },
      ));
    if (type == SongItemType.Diss) {
    } else {
      widgets
        ..add(InkWellContainer(
          image: Icon(
            Icons.delete_forever,
            color: themeColor,
            size: _iconSize,
          ),
          text: "删除",
          onTap: () {
            showCommonDialog(
              context,
              content: "是否将此歌曲删除？",
              onConfirm: () {
                if (type == SongItemType.Gedan) {
                  GedanServices.getInstace()
                      .deleteSongFromGedan(gedanId, song.hash);
                } else if (type == SongItemType.Local) {
                  SongServices.getInstace().deleteLocalSong(song);
                } else {
                  SongServices.getInstace().deleteRecentSong(song.hash);
                }
                Navigator.of(context).pop();
                Navigator.of(context).pop();
              },
            );
          },
        ));
    }

    if (!song.isLocal) {
      widgets.add(InkWellContainer(
        image: Icon(
          Icons.cloud_download,
          color: themeColor,
          size: _iconSize,
        ),
        text: "下载",
        onTap: () {
          SongServices.getInstace().downloadSong(song);
        },
      ));
    }
    return widgets;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(top: 12, bottom: 12, left: 16, right: 16),
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(
                    width: 0.4, color: Color.fromRGBO(200, 200, 200, 1)),
              ),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Expanded(
                  child: Row(
                    children: <Widget>[
                      Container(
                        margin: EdgeInsets.only(right: 10),
                        child: ClipRRect(
                          child: song.img == null
                              ? Image.asset(
                                  "images/nvhai.png",
                                  width: 60,
                                )
                              : Image.network(
                                  "${song.img}",
                                  width: 60,
                                ),
                          borderRadius: BorderRadius.circular(8),
                        ),
                      ),
                      Expanded(
                        child: SongDetail(
                          singerName: song.singerName,
                          songName: song.songName,
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: 8.0),
                  child: Column(
                    children: <Widget>[
                      Image.asset(
                        "images/${song.platform}.png",
                        width: 24,
                        height: 24,
                      ),
                      Text(
                        "来 源",
                        style: TextStyle(fontSize: 10),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
          Column(
            children: this._renderClick(context),
          )
        ],
      ),
    );
  }
}

class SongDetail extends StatelessWidget {
  final String songName;
  final String singerName;

  const SongDetail({Key key, this.songName, this.singerName}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Color wordColor = Util.wordColor;
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: Text(
            "$songName",
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
              fontSize: 16,
            ),
          ),
        ),
        SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: Text(
            "$singerName",
            overflow: TextOverflow.ellipsis,
            style: TextStyle(fontSize: 14, color: wordColor),
          ),
        ),
      ],
    );
  }
}
